import json
import random
import time
from pprint import pprint
import requests
from lxml import etree

from crawler_core.constant.constants import Constants
from crawler_core.constant.constants_api import ConstantAPI
from crawler_core.exception.cookies_exec import CookieException
from crawler_core.exception.search_vertify_exec import SearchVertifyException
from crawler_core.exception.unknown_exec import UnknownException
from crawler_core.exception.request_exec import RequestException
from lib.sys.logger_factory import LoggerFactory
from search_server.interface_search import SearchInterface
from search_server.template.template_51job import Property51job


class Job51SearchResume(SearchInterface):
    logger = LoggerFactory.getLogger("Job51SearchResume", "search")

    def __init__(self):
        pass

    def search(self, context, cookies):
        """开始搜索"""
        # 获取列表页参数
        detail_data_list = []
        # {'id': 'hjvgjygfojuyg', 'keyword': 'java', 'city': '上海', 'workYear': [1, 3], 'education': '本科'}
        cropcode = context.get(Constants.CONTEXT_CORP_CODE)
        condition = context.get(Constants.CONTEXT_SEARCH_CONDITION)
        keyword = context.get(Constants.CONTEXT_SEARCH_CONDITION).get("keyword")
        city = context.get(Constants.CONTEXT_SEARCH_CONDITION).get("city")
        workYear = context.get(Constants.CONTEXT_SEARCH_CONDITION).get("workYear")
        education = context.get(Constants.CONTEXT_SEARCH_CONDITION).get("education")
        page = context.get(Constants.CONTEXT_CUR_PAGE)
        count = context.get(Constants.CONTEXT_PAGE_SIZE)
        parms = {
            '__EVENTTARGET': 'pagerBottomNew$btnNum{}'.format(str(page)),
            'pagerTopNew$ctl06': '15',
            '__VIEWSTATE': '/wEPDwUJMzM5MDQ0NzY5DxYOHgRJc0VOaB4QdnNTZWxlY3RlZEZpZWxkcwUzQUdFLFdPUktZRUFSLFNFWCxBUkVBLFdPUktGVU5DLFRPUERFR1JFRSxMQVNUVVBEQVRFHghQYWdlU2l6ZQIyHgxzdHJTZWxlY3RDb2wFM0FHRSxXT1JLWUVBUixTRVgsQVJFQSxXT1JLRlVOQyxUT1BERUdSRUUsTEFTVFVQREFURR4JUGFnZUluZGV4Zh4IUGFnZURhdGEF2hAxfDIwMHwyMDB8NTAwMHwzODM1NjI2MDQsMzgzM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UGljX2JhawUJY2hrSGFzUGljBQxjYnhDb2x1bW5zJDAFDGNieENvbHVtbnMkMQUMY2J4Q29sdW1ucyQyBQxjYnhDb2x1bW5zJDMFDGNieENvbHVtbnMkNAUMY2J4Q29sdW1ucyQ1BQxjYnhDb2x1bW5zJDYFDGNieENvbHVtbnMkNwUMY2J4Q29sdW1ucyQ4BQxjYnhDb2x1bW5zJDkFDWNieENvbHVtbnMkMTAFDWNieENvbHVtbnMkMTEFDWNieENvbHVtbnMkMTIFDWNieENvbHVtbnMkMTI=',
            'ctrlSerach$search_keyword_txt': keyword,
            # 'ctrlSerach$hidDegreeArea': education,
            # 'ctrlSerach$search_expjobarea_input': city,
            # 'ctrlSerach$search_expjobarea_hid': '上海|020000',
            'ctrlSerach$hidSearchValue': '##0###3-4 年|5#3-4 年|5#本科|7#本科|##############近1年|6##1#0##上海|020000#0#0#0',
        }
        # pprint(parms)
        Job51SearchResume.logger.info("开始抓取关键词：%s" % keyword)
        # 请求列表页， 获取详情页URL
        try:
            detail_url_list, session, proxies = self.request_search_url(parms, cookies, count)
        except CookieException:
            Job51SearchResume.logger.info("Job51SearchResume 开始查询简历列表页 cookies 失效...")
            data = {Constants.SEARCH_CODE: ConstantAPI.RETURN_ERROR_COOKIES, Constants.CONTEXT_CORP_CODE: cropcode}
            return data
        except UnknownException as e:
            Job51SearchResume.logger.info("Job51SearchResume 开始查询简历列表页 出现未知错误...")
            data = {
                Constants.SEARCH_CODE: ConstantAPI.RETURN_ERROR_UNKNOWN,
                Constants.SEARCH_MSG: json.dumps(e),
                Constants.CONTEXT_CORP_CODE: cropcode
            }
            return data
        # 请求详情页，获取HTML
        pprint(detail_url_list)
        for detail_url in detail_url_list:
            print("Job51SearchResume---简历详情页url:%s" % detail_url)
            detail_data = {
                "detail_url": detail_url,
                "corpcode": context.get(Constants.CONTEXT_CORP_CODE),
                "accountno": context.get(Constants.CONTEXT_SEARCH_CONDITION).get("id"),
                "username": context.get(Constants.CONTEXT_USERNAME),
                "password": context.get(Constants.CONTEXT_PASSWORD),
                "account": context.get(Constants.CONTEXT_ACCOUNT),
            }
            # pprint(detail_data)
            try:
                detail_html = self.request_detail_url(detail_url, session, proxies)
            except SearchVertifyException:
                Job51SearchResume.logger.info("Job51SearchResume 开始查询简历详情页 出现跳转验证码...")
                data = {
                    Constants.CACHE_SEARCH_DATA: detail_url,
                    Constants.SEARCH_CODE: ConstantAPI.RETURN_ERROR_VERIFY,
                    Constants.CONTEXT_CORP_CODE: cropcode
                }
                return data
            except CookieException:
                Job51SearchResume.logger.info("Job51SearchResume 开始查询简历详情页 cookies 失效...")
                data = {Constants.SEARCH_CODE: ConstantAPI.RETURN_ERROR_COOKIES,
                        Constants.CONTEXT_CORP_CODE: cropcode}
                return data
            except UnknownException as e:
                Job51SearchResume.logger.info("Job51SearchResume 开始查询简历详情页 出现未知错误...")
                data = {
                    Constants.SEARCH_CODE: ConstantAPI.RETURN_ERROR_UNKNOWN,
                    Constants.SEARCH_MSG: json.dumps(e),
                    Constants.CONTEXT_CORP_CODE: cropcode
                }
                return data
                pass

            detail_data["detail_html"] = detail_html
            detail_data_list.append(detail_data)
            # time.sleep(random.randint(2, 5))
        data = {
            Constants.CACHE_SEARCH_DATA: detail_data_list, Constants.SEARCH_CODE: ConstantAPI.RETURN_SUCCESS,
            Constants.CACHE_SEARCH_CONDITION: condition
        }
        # pprint(data)
        # with open('data.json', 'w') as f:
        #     f.write(json.dumps(data, indent=2, ensure_ascii=False))
        return data

    def request_search_url(self, parms, cookies, count):
        """请求列表页url, 获取详情页url"""
        search_url = Property51job.search_url
        search_headers = Property51job.search_header
        proxies = Property51job.proxies
        session = requests.Session()
        session.keep_alive = False
        for cookie in cookies:
            session.cookies.set(cookie["name"], cookie["value"])
        response = session.post(url=search_url, headers=search_headers, data=parms, allow_redirects=False, verify=False,
                                proxies=proxies, timeout=30)
        # 判断cookie 是否有效
        print("Job51SearchResume--请求列表页响应状态码--%s" % response.status_code)
        if response.status_code != 200:
            Job51SearchResume.logger.info("Job51SearchResume 搜索失败，cookie失效，帮我重新登录一下-----")
            raise CookieException()

        search_html = etree.HTML(response.text)
        detail_url_data_list = search_html.xpath('//td[@class="Common_list_table-id-text"]/span/a/@href')
        detail_url_list = []
        num = 0
        for detail_url_data in detail_url_data_list:
            detail_url = 'https://ehire.51job.com' + detail_url_data
            Job51SearchResume.logger.info("详情页url:%s" % detail_url)
            detail_url_list.append(detail_url)
            num += 1
            if num >= int(count):
                break
        return detail_url_list, session, proxies
        pass

    def request_detail_url(self, detail_url, session, proxies):
        """请求详情页获取html"""

        response = session.get(url=detail_url, headers=Property51job.detail_header, allow_redirects=False, verify=False,
                               proxies=proxies, timeout=60)
        # 判断cookie 是否有效
        print("Job51SearchResume--请求详情页响应状态码--%s" % response.status_code)
        if response.status_code != 200:
            Job51SearchResume.logger.info("Job51SearchResume 搜索失败，cookie失效，帮我重新登录一下-----")
            raise CookieException()
        detail_html = response.text
        # 这里面要进行简历爬取页面的判断有没有出现验证码
        html = etree.HTML(detail_html)
        message = "".join(html.xpath('//input[@id="hidShowCode"]/@value'))
        if message == '1':
            Job51SearchResume.logger.info("Job51SearchResume 搜索失败，查询出现跳转验证码，请及时处理-----")
            raise SearchVertifyException()
        return detail_html

# 本机测试
# if __name__ == '__main__':
#     context = {
#         "keyword": "java",
#         "page": 1,
#         "count": 10,
#         "corpcode": "JCL0000002",
#         "accountno": "393BE10D636D4883BD94B18843354317",
#         "username": "jucailin02",
#         "password": "jucailin02!!",
#         "account": "上海晓家网络科技",
#     }
#     print(context)
#     cookies = [{'name': 'guid', 'value': '74c2f23d5bd1de68cace16bd3a61dc71'},
#                {'name': 'EhireGuid', 'value': '300c20d6308e4e2cbe6312f89bac63a7'},
#                {'name': 'LangType', 'value': 'Lang=&Flag=1'},
#                {'name': 'slife', 'value': 'lowbrowser%3Dnot%26%7C%26'},
#                {'name': 'adv',
#                 'value': 'adsnew%3D0%26%7C%26adsresume%3D1%26%7C%26adsfrom%3Dhttps%253A%252F%252Fwww.baidu.com%252Fs%253Fie%253Dutf-8%2526f%253D8%2526rsv_bp%253D0%2526rsv_idx%253D1%2526tn%253Dbaidu%2526wd%253D51job%2526rsv_pq%253Dfb046229000a688e%2526rsv_t%253D3e62Dentwa4s65LjgrJUShuruTJKBF2m5XNTPdESg0en9Tx12duhL8SKzN8%2526rqlang%253Dcn%2526rsv_enter%253D1%2526rsv_sug3%253D2%2526rsv_sug1%253D2%2526rsv_sug7%253D101%26%7C%26adsnum%3D2004282'},
#                {'name': 'ps', 'value': 'needv%3D1'},
#                {'name': 'ASP.NET_SessionId', 'value': 'iywpzerrwh13qbsyw0szulwu'},
#                {'name': 'AccessKey', 'value': '80c4e99ee70f482'},
#                {'name': 'HRUSERINFO',
#                 'value': 'CtmID=1646056&DBID=2&MType=02&HRUID=1962459&UserAUTHORITY=1100111000&IsCtmLevle=1&UserName=fanwei&IsStandard=0&LoginTime=12%2f19%2f2018+09%3a56%3a15&ExpireTime=12%2f19%2f2018+10%3a06%3a15&CtmAuthen=0000011000000001000110010000000011100001&BIsAgreed=true&IsResetPwd=0&CtmLiscense=11&AccessKey=cce9b42df8998c51&source=0'}]
#     spider = Job51SearchResume()
#     spider.search(context, cookies)
